﻿using System;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Claudia De Leon
////Date created: 04/10/2013
////ULTI-94694 Location Business Rules-Edit a Global Location in .NET https://ultidev/browse/ULTI-94694
////*************************************************************************************************************************************

namespace Echo.Automation.Compliance.BusinessRules.Locations
{
    [TestFixture]
    [Author("Claudia De Leon", "claudia_deleon@ultimatesoftware.com")]
    [Category("Business Rules")]
    [Header("Config")]
    [Config(".NET")]
    public class EditGlobalLocation : MbUnitAutomatedTest
    {
        #region Setups / Teardowns
        UltiPro UltiPro;
        UltiProDatabaseContext Database;
        private readonly DateTime Date = DateTime.Today;
        private const string AUGLB = "AUGLB";

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            #region Setup database ULTIPRO_CALENDAR
            UltiPro = Launch<UltiPro>();
            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            Database.Company.AddRollBack("iLocation", string.Format("code = '{0}'", AUGLB));
            Database.Company.AddRollBack("Loccomp", string.Format("lcpCode = '{0}'", AUGLB));
            Database.Company.AddRollBack("Addresses", string.Format("line1 = '{0}'", "Address Line 1"));
            #endregion

            #region Login as System Admin MerrillG for Web Payroll Company
            UltiPro.Pages.Login.LoginAs("MerrillG", "password", Config.LanguageCode);
            UltiPro.Pages.CompanySelector.ChooseCompanies("CALEN", "C0014");
            #endregion
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            Database.Company.RollBackDatabase();
            UltiPro.Browser.Quit();
        }

        [SetUp]
        public void SetUp()
        {
            #region Open/Edit Global Location Australia Global, filter by code AUGLB
            UltiPro.Macros.Navigate.ToLocationBusinessRule("AUGLB");
            #endregion
        }
        #endregion

        [Test]
        public void EditableFieldsOnAddChangeLocation()
        {
            #region GIVEN that I am on the Edit page for Australia Location Business Rules
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(header => header.PageHeaderLabel, "innerHTML", "Add/Change Location");
            UltiPro.Pages.AddChgLocations.Is(page => page.LocationCountryValue, "innerHTML", "Australia");
            UltiPro.Pages.AddChgLocations.Is(page => page.CodeValue, "innerHTML", AUGLB);
            UltiPro.Pages.AddChgLocations.Is(page => page.DescriptionTextbox.EnglishTextBox, "value", "Australia Automation");
            #endregion

            #region WHEN the page is on Edit mode
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Reset);
            UltiPro.Pages.AddChgLocations.ButtonBar.Exists(button => button.Cancel);
            #endregion

            #region THEN the page displays all fields in edit mode except for Location Country and Code
            UltiPro.Pages.AddChgLocations.NotExists(page => page.LocationCountryDropdown);
            UltiPro.Pages.AddChgLocations.NotExists(page => page.CodeTextbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.DescriptionTextbox.EnglishTextBox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.InactiveDate.Enabled();
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactCountryDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactAddressLine1Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactAddressLine2Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactAddressLine3Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactAddressLine4Texbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactCityTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactStateProvinceDropdown);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactZipPostalCodeTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ContactNameTexbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.PhoneNumberTextbox);
            UltiPro.Pages.AddChgLocations.Enabled(page => page.ExtensionTextbox);
            UltiPro.Pages.AddChgLocations.Enabled(x => x.CompaniesOptionMover.Source);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        public void RequiredFieldsOnAddChangeLocation()
        {
            #region GIVEN that I am on the Edit page for Australia Location Business Rules
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(page => page.CodeValue, "innerHTML", AUGLB);
            UltiPro.Pages.AddChgLocations.Is(page => page.DescriptionTextbox.EnglishTextBox, "value", "Australia Automation");
            #endregion

            #region WHEN I clear the Descritpion textbox and move all the selected compnies to the available box
            UltiPro.Pages.AddChgLocations.Clear(page => page.DescriptionTextbox.EnglishTextBox);
            UltiPro.Pages.AddChgLocations.CompaniesOptionMover.Click(button => button.ArrowLeftAll);
            #endregion

            #region AND attempt to save the page
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion

            #region THEN there is an error message:"Please correct the 2 highlighted field(s) before continuing"
            UltiPro.Pages.AddChgLocations.ErrorMessages.Has("Please correct the 2 highlighted field(s) before continuing");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        [Test]
        public void ResetFields()
        {
            #region GIVEN that I am on the Edit page for Australia Location Business Rules
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(page => page.CodeValue, "innerHTML", AUGLB);
            #endregion

            #region AND I complete the State/Province, Zip Code and Phone number fields under Contact Information
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactCountryDropdown, "Australia", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactStateProvinceDropdown, "Tasmania", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactZipPostalCodeTexbox, "26571");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhoneNumberTextbox, "(786)258-3382");
            #endregion

            #region WHEN I click the Reset button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Reset);
            #endregion

            #region THEN the completed fields no longer contain data
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactStateProvinceDropdown, "innerHTML", "");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactZipPostalCodeTexbox, "value", "");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhoneNumberTextbox, "value", "");
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Cancel);
            UltiPro.Alert.Accept();
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion
        }

        //Run last on the suite for teardown purposes
        [Test(Order = 1)] 
        public void CompleteAllEditableFields()
        {
            #region GIVEN that I am on the Edit page for Australia Location Business Rules
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Edit);
            UltiPro.Pages.AddChgLocations.Is(page => page.CodeValue, "innerHTML", AUGLB);
            #endregion

            #region AND Complete all the editable fields on the page
            UltiPro.Pages.AddChgLocations.Click(page => page.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.InactiveDate.Set(Date);
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine2Texbox, "Addy Line 2");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine3Texbox, "Addy Line 3");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhyicalAddressLine4Texbox, "Addy Line 4");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactCountryDropdown, "Australia", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactAddressLine1Texbox, "Addy Line 1.1");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactAddressLine2Texbox, "Addy Line 2.1");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactAddressLine3Texbox, "Addy Line 3.1");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactAddressLine4Texbox, "Addy Line 4.1");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactStateProvinceDropdown, "Victoria", SelectOption.Text);
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactZipPostalCodeTexbox, "26571");
            UltiPro.Pages.AddChgLocations.Set(page => page.ContactNameTexbox, "Rose Ann");
            UltiPro.Pages.AddChgLocations.Set(page => page.PhoneNumberTextbox, "(786)258-3382");
            UltiPro.Pages.AddChgLocations.Set(page => page.ExtensionTextbox, "+25");
            #endregion

            #region WHEN I click on the Save button
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Save);
            #endregion

            #region THEN the page displays all the saved fields
            UltiPro.Pages.AddChgLocations.Selected(page => page.InactiveCheckbox);
            UltiPro.Pages.AddChgLocations.Is(page => page.InactiveDateValue, "innerHTML", Date.ToString("MM/dd/yyyy"));
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine2Value, "innerHTML", "Addy Line 2");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine3Value, "innerHTML", "Addy Line 3");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhyicalAddressLine4Value, "innerHTML", "Addy Line 4");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactCountryValue, "innerHTML", "Australia");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactAddressLine1Value, "innerHTML", "Addy Line 1.1");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactAddressLine2Value, "innerHTML", "Addy Line 2.1");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactAddressLine3Value, "innerHTML", "Addy Line 3.1");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactAddressLine4Value, "innerHTML", "Addy Line 4.1");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactStateProvinceValue, "innerHTML", "Victoria");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactZipPostalCodeValue, "innerHTML", "26571");
            UltiPro.Pages.AddChgLocations.Is(page => page.ContactNameValue, "innerHTML", "Rose Ann");
            UltiPro.Pages.AddChgLocations.Is(page => page.PhoneNumberValue, "innerHTML", "7862583382");
            UltiPro.Pages.AddChgLocations.Is(page => page.ExtensionValue, "innerHTML", "+25");
            UltiPro.Pages.AddChgLocations.Is(page => page.CompaniesUsingThisTaxLocationList(1), "innerHTML", "International Company-Aus'sie.Testing1");
            #endregion

            #region AND the Menu Bar buttons are Back, Edit, Delete, Print, Help
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Edit);
            UltiPro.Pages.AddChgLocations.ButtonBar.Enabled(button => button.Delete);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Back);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Print);
            UltiPro.Pages.AddChgLocations.ButtonBar.Visible(button => button.Help);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotExists(button => button.Save);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotExists(button => button.Reset);
            UltiPro.Pages.AddChgLocations.ButtonBar.NotVisible(button => button.Cancel);
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Back);
            #endregion

            #region Database check
            /*Database.Company.Verify("SELECT loccode, LocDesc, LocName, LocIsInactive, LocAddressLine1, LocAddressLine2, LocAddressLine3, " +
                                    "LocAddressLine4, LocAddressCity, LocAddressState, LocAddressZipcode, LocContact, LocPhoneMainNumber, LocPhoneMainExt " +
                                    "FROM location WHERE loccode = '" + AUGLB + "' and LocDesc = 'Australia' AND LocName = 'Australia'" +
                                     "AND LocIsInactive = 'Y' AND LocAddressLine1 = 'Wallaby 42' AND LocAddressLine2 = 'Addy Line 2' AND" +
                                     " LocAddressLine3 = 'Addy Line 3' AND LocAddressLine4 = 'Addy Line 4' " +
                                     "AND LocAddressCity = 'Sidney' AND LocAddressState = 'VIC' AND LocAddressZipcode ='26571' " +
                                     "AND LocContact = 'Rose Ann' AND LocPhoneMainNumber = '(786)258-3382' AND LocPhoneMainExt = '+25'", "LocationBusinessRules/CompleteAllEditableFields.xml");
            Database.Company.Verify("SELECT CmpCompanyName, CmpCoID FROM Loccomp JOIN Company on CmpCoID = lcpcoid WHERE lcpCode = '" + AUGLB + "' " +
                                    "ORDER BY lcpcode", "LocationBusinessRules/CMPCompleteAllEditableFields.xml");
            var dbDate = Database.Company.GetValueAsDateTime("SELECT LocInactiveDate FROM location where loccode = '" + AUGLB + "'").ToShortDateString();
            Assert.AreEqual(Date.ToShortDateString(), dbDate);*/
            #endregion
        }

        [Test(Order = 2)]
        public void DeleteLocationAustralia()
        {
            #region GIVEN I open a location with Code AUGLB
            UltiPro.Pages.AddChgLocations.Is(code => code.CodeValue, "innerHTML", "AUGLB");
            #endregion

            #region WHEN I Delete the location using the Delete button on the menu bar
            UltiPro.Pages.AddChgLocations.ButtonBar.Click(button => button.Delete);
            UltiPro.Alert.Accept();
            #endregion

            #region THEN the location is no longer available on the Summary grid
            UltiPro.Pages.LocationSummary.Is(header => header.PageHeaderLabel, "innerHTML", UltiPro.Localization.Strings.Locations);
            UltiPro.Pages.LocationSummary.LocationSummaryGrid.Find(code => code.CodeFilter, FilterType.Is, "AUGLB");
            UltiPro.Pages.LocationSummary.LocationSummaryGrid.Row(column => column.CodeLink, "AUGLB").RowNotExists();
            #endregion

            #region Database check
            //Database.Company.Verify("SELECT COUNT (loccode) FROM location WHERE loccode = 'AUGLB'", "LocationBusinessRules/DeleteLocationAustralia.xml");
            #endregion
        }
    }
}
